import sys

from PyQt5.QtCore import QTime, QTimer
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QMessageBox
import a026.a026 as ljyui

'''
self.counter = QTime()  # 计时器
self.counter.restart()
total_ms = self.counter.elapsed()  # 总毫秒数

self.timer1 = QTimer()  # 定时器
self.timer1.timeout.connect(self.timer1_timeout)  # 定时器任务槽函数
self.timer1.start(1000)
self.timer1.stop()

a, b = divmod(total_ms, 1000)  # 一步获得 整商和余数
'''


class MyClass(QWidget, ljyui.Ui_Form):
    def __init__(self):
        super().__init__()
        self.counter = QTime()  # 计时器
        self.timer1 = QTimer()  # 定时器
        self.initUI()

    def initUI(self):
        self.setupUi(self)  # ui 界面
        self.setWindowTitle('刘金玉编程教程')
        self.setWindowIcon(QIcon('./img/msn.ico'))

        self.pushButton.clicked.connect(self.btn_start)
        self.timer1.timeout.connect(self.timer1_timeout)

        self.show()

    def btn_start(self):
        if self.pushButton.text() == '开始计时':
            self.counter.restart()
            self.timer1.start(31)
            self.pushButton.setText('停止')
        else:
            self.pushButton.setText('开始计时')
            self.timer1.stop()

    def timer1_timeout(self):
        total_ms = self.counter.elapsed()  # 总毫秒数
        a, b = divmod(total_ms, 1000)
        self.lcdms.display(b)
        a, b = divmod(a, 60)
        self.lcdsec.display(b)
        a, b = divmod(a, 60)
        self.lcdmin.display(b)
        _, b = divmod(a, 60)  # _ 丢弃忽略
        self.lcdhour.display(b)

        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myf = MyClass()
    app.exec_()
